home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Almathera Ten Pack 2: CDPD 1
/
Almathera Ten on Ten - Disc 2: CDPD 1.iso
/
pd
/
176-200
/
179
/
unixutil
/
detab.c
< prev
next >
Wrap
C/C++ Source or Header
|
1995-03-13
|
3KB
|
132 lines
/* detab.c - expand tabs to spaces in a file. If a single tabstop *
* is given, tabs are tabstop spaces apart, otherwise tabstops *
* are at <tab1>, <tab2>,..., <tabn>. If no file is specified, *
* standard input is read and standard output written. *
* *
* detab [-<tab1> -<tab2>... -<tabn>] [filename... ] *
* *
* detab (C) 1988 by Gary L. Brant *
* *
* :ts=8 */
#define MAXLINE 1000
#include <stdio.h>
char tabarray[MAXLINE];
main (argc, argv) /* remove trailing blanks and tabs and */
/* expand tabs in source lines */
int argc;
char *argv[];
{
int i = 0, j, k, l, ntabs = 0, tabstop = 8;
int argtabs[99];
char ch, *pch;
FILE *ifile, *fopen ();
/* decode tabstop arguments */
while ((++i < argc) && (argv[i][0] == '-')) {
j = 1;
tabstop = 0;
while ((ch = argv[i][j++]) != '\0') {
if (ch >= '0' && ch <= '9') {
tabstop *= 10;
tabstop += ch - '0';
} else {
fputs ("bad args\n", stderr);
exit (20);
}
}
argtabs[ntabs++] = tabstop;
}
/* fill tabarray with \1 for each tabstop position */
for (k = 0; k < MAXLINE; k++)
tabarray[k] = '\0';
if (ntabs > 1)
for (k = 0; k < ntabs; k++)
if ((l = argtabs[k]-1) < MAXLINE)
tabarray[l] = '\001';
else {
fputs ("bad tab specification\n", stderr);
exit (20);
}
else if ((tabstop > 0) && (tabstop < MAXLINE))
for (k = tabstop; k < MAXLINE; k += tabstop)
tabarray[k] = '\001';
else {
fputs ("bad tab specification\n", stderr);
exit (20);
}
/* remaining arguments should be file names - detab them */
if (i < argc) {
while (i < argc)
if ((ifile = fopen ((pch = argv[i++]), "r")) == NULL) {
fputs ("detab: cant open ", stderr);
fputs (pch, stderr);
putc ('\n', stderr);
exit (20);
} else
detab (ifile);
} else
detab (stdin);
}
/* detab - remove the tabs from one file */
detab (ifile)
FILE *ifile;
{
int n;
char inline[MAXLINE], outline[MAXLINE], *fgets ();
while ((fgets (inline, MAXLINE, ifile)) != NULL) {
n = strlen (inline);
while (--n >= 0) /* back over white space */
if (inline[n] != ' ' && inline[n] != '\t' &&
inline[n] != '\n') break;
inline[n+1] = '\0';
expand (inline, outline, MAXLINE);
puts (outline);
}
}
/* expand - expand a line */
expand (in, out, lim)
char in[], out[];
int lim;
{
register int i, j;
register char ch;
i = j = 0;
while (((ch = in[i++]) != '\0') && (j < lim)) {
if (ch == '\t') {
register int k;
register char tc;
k = j;
out[j++] = ' ';
while ((j < lim) && ((tc = tabarray[j]) == '\0'))
out[j++] = ' ';
if (tc == '\0') {
j = k;
out[j++] = ch;
}
} else
out[j++] = ch;
}
out[j] = ch;
}
/* wb_parse is included here only to reduce the size of the executable */
void
_wb_parse ()
{
}